package me.macd.mindoc.util;

public class DefaultNameHandler implements NameHandler {
    @Override
    public String columnName(String fieldName) {
        // 将驼峰命名转成下划线命名
        StringBuilder sb = new StringBuilder();

        for (Character c : fieldName.toCharArray()) {
            if (Character.isUpperCase(c)) {
                sb.append("_").append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }

        if (sb.charAt(0) == '_' && sb.charAt(sb.length() - 1) != '_') {
            return sb.substring(1);
        } else if (sb.charAt(0) == '_' && sb.charAt(sb.length() - 1) == '_') {
            return sb.substring(1, sb.length() - 1);
        } else if (sb.charAt(0) != '_' && sb.charAt(sb.length() - 1) == '_') {
            return sb.substring(0, sb.length() - 1);
        } else {
            return sb.toString();
        }
    }
}
